const {setR,getR,delR,caseTime} = require('./redisServe');
const {Message} = require('../model/index');
const {avaterUrl} = require('../routerConfig')
const length = avaterUrl.length;
exports.getMessage = async (limit=10,skip=1)=>{

    const result = await Message.find({ isShow: true})
                  .skip(limit*(skip-1)).limit(limit);
    const count = await Message.find({ isShow: true}).count();
    return {
        row:result,
        count
    };
}

exports.addMessage = async (name,content)=>{

    if(!(name && content)) return '留言失败，必要条件缺失';
    const thumb =  avaterUrl[Math.floor(Math.random()*length)];
    const result = await Message.create({
        name,
        content,
        thumb
    });
    return result;
}

exports.delMessage= async (_id)=>{

    const result = await Message.deleteOne({_id})
    return result;
}

exports.setMessageShow = async (_id,isShow) => {
    const result = await Message.updateOne({_id},{
        $set:{
            isShow
        }
    })
    return result
}

exports.getMessageAdmin = async (limit = 10,skip = 1)=>{

    // const Cr = await getR()
    const result = await Message.find()
                  .skip(limit*(skip-1)).limit(limit);
    const count = await Message.find().count();
    return {
        row:result,
        count
    };
}